package com.evil.common.application.enums.judge;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 提交校验符号
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum JudgeSignEnum {
    /**
     * 判断符号，0:等于, 1:不等于, 2:包含, 3:不包含, 4:属于, 5:不属于
     */
    EQUALS(0, "等于"),
    UN_EQUALS(1, "不等于"),
    CONTAIN(2, "包含"),
    UN_CONTAIN(3, "不包含"),
    BELONG(4, "属于"),
    UN_BELONG(5, "不属于"),
    GT(6, "大于"),
    GTE(7, "大于等于"),
    LT(8, "小于"),
    LTE(9, "小于等于"),
    SCOPE(10, "范围"),
    SUBORDINATE(11, "从属"),
    UN_SUBORDINATE(12, "不从属"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<JudgeSignEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static JudgeSignEnum isExistById(Integer id) {
        return JudgeSignEnum.findById(id).orElseThrow(JudgeSignEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "判断符号参数错误");
    }
}
